package com.southwind.service.impl;

import com.alibaba.excel.EasyExcel;
import com.southwind.entity.AdEffect;
import com.southwind.configuration.BTrainPlanExcelListener;
import com.southwind.mapper.AdInfoMapper;
import com.southwind.mapper.AdPlatformMapper;
import com.southwind.mapper.AdEffectMapper;
import com.southwind.service.AdEffectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.southwind.util.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@Service
public class AdEffectServiceImpl extends ServiceImpl<AdEffectMapper, AdEffect> implements AdEffectService {
    @Autowired
    private AdInfoMapper adInfoMapper;
    @Autowired
    private AdPlatformMapper adPlatformMapper;

    @Autowired
    private AdEffectMapper adEffectMapper;
    @Override
    public List<String> listAdNames() {
        return adInfoMapper.selectName();
    }

    @Override
    public List<String> listAdPlatforms() {
        return adPlatformMapper.selectName();
    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        List<AdEffect> list = adEffectMapper.selectList(null);
        List<AdEffect> productVOList=new ArrayList<>();
        for (AdEffect product : list) {
            AdEffect productExcelVO = new AdEffect();
            BeanUtils.copyProperties(product, productExcelVO);
            productVOList.add(productExcelVO);
        }
        ExcelUtil.exportExcel(response, productVOList, AdEffect.class, "广告成效列表", "广告成效列表");
    }
    @Override
    public void importExcel(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), AdEffect.class, new BTrainPlanExcelListener(adEffectMapper)).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
